package com.jdew.thkj.base;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jaeger.library.StatusBarUtil;
import com.jdew.thkj.dialog.CommonDialog;
import com.jdew.thkj.manager.WrapContentLinearLayoutManager;

import butterknife.ButterKnife;

/**
 * Created by XOne on 2016/9/19.
 */
public abstract class BaseFragment extends Fragment {
    public Activity mActivity;
    protected boolean isVisible;
    private boolean isPrepared;
    private boolean isFirst = true;
    public Bundle savedInstanceState;
    public SharedPreferences sp;
    public SharedPreferences.Editor editor;
    public String key;
    public String time;
    protected abstract int getContentViewId();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        this.savedInstanceState=savedInstanceState;
        mActivity = getActivity();// 获取所在的activity对象
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView=inflater.inflate(getContentViewId(),container,false);
       /*注解框架初始化*/
        ButterKnife.bind(this, rootView);
        onCreateViewT();
        StatusBarUtil.setTranslucentForImageViewInFragment(mActivity,0,null);
        return rootView;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                lazyLoad();
            }
        },200);
    }

    public void toset(String message) {
        new CommonDialog.Builder(mActivity)
                .setTitle("提示")
                .setMessage(message)
                .setdialogPostive("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                }).show();
    }
    protected abstract void onCreateViewT();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
          /*解绑*/
        ButterKnife.unbind(this);
        System.gc();
    }
    public <T extends RecyclerView.Adapter>void setRcycleview(RecyclerView rcycleview, T adapter){
        rcycleview.setLayoutManager(new WrapContentLinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        rcycleview.setAdapter(adapter);
    }
    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mActivity, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
//        Log.d("TAG", "fragment->setUserVisibleHint");
        if (getUserVisibleHint()) {
            isVisible = true;
            lazyLoad();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirst) {
            return;
        }
        initData();
        isFirst = false;
    }

    public abstract void initData();
    protected void onInvisible(){}
}
